package com.colin.distributelock.controller;

import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * Created by Colin on 2020/8/31 19:55
 * email: colinzhaodong@gmail.com
 * desc:
 *
 * @author zhaod
 */
@RestController
@Slf4j
public class RedissonLockController {
	@Resource
	private RedissonClient redisson;


	@GetMapping("redisson-lock")
	public String redisLock() {
		RLock rLock = redisson.getLock("order");
		try {
			rLock.lock(30, TimeUnit.SECONDS);
			log.info("我获得了锁");
			TimeUnit.SECONDS.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			log.info("我释放了锁");
			rLock.unlock();
		}
		return "方法执行完成";
	}
}
